package thread;

import java.util.concurrent.TimeUnit;

public class DeiSynchronizedTest extends Thread{

    public static void main(String[] args) {
        DeiSynchronizedTest diedReentrantLock = new DeiSynchronizedTest(1, "码农");
        DeiSynchronizedTest diedReentrantLock1 = new DeiSynchronizedTest(2, "马云");
        diedReentrantLock.start();
        diedReentrantLock1.start();
        diedReentrantLock.interrupt();
        diedReentrantLock1.interrupt();

    }
    int i = 0;

    public DeiSynchronizedTest(int i,String name) {
        super(name);
        this.i = i;
    }

    @Override
    public void run() {
        if (i==1){
            synchronized (DeiSynchronizedTest.class){
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (Thread.currentThread().isInterrupted()){
                    return;
                }
                synchronized (Object.class){
                    if (Thread.currentThread().isInterrupted()){
                        return;
                    }
                }
            }
        }else if (i==2){
            synchronized (Object.class){
                try {
                    TimeUnit.SECONDS.sleep(1);
                    if (Thread.currentThread().isInterrupted()){
                        return;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (DeiSynchronizedTest.class){
                    if (Thread.currentThread().isInterrupted()){
                        return;
                    }
                }
            }
        }
    }
}
